using System;
using System.Linq;
using LaTrompa.Validation;

namespace CoverageRunner
{
    public class ArgumentParser
    {
        private string[] _args;

        public string TestRunner 
        {
            get 
            {
                return getValueFromArguments("-runner:");
            }
        }

        private string getValueFromArguments(string argument)
        {
            var value = _args.ToList().Find(a => a.StartsWith(argument));
            return value == null ? null : value.Replace(argument, string.Empty);
        }

        public string[] Tests
        {
            get
            {
                return getValueFromArguments("-tests:").Split(',');
            }
        }

        public string[] Assemblies
        {
            get
            {
                return getValueFromArguments("-assemblies:").Split(',');
            }
        }

        public string VsTsInstrLocation 
        { 
            get
            {
                return getValueFromArguments("-vstsinstrpath:");
            } 
        }

        public string PathCodeToInstrument 
        { 
            get
            {
                return getValueFromArguments("-pathtocode:");
            }
        }

        public string ExePath
        {
            get { return getValueFromArguments("-exepath:"); }
        }

        public string SymbolsPath
        {
            get { return getValueFromArguments("-symbolspath:"); }
        }

        public string OutputHtml
        {
            get { return getValueFromArguments("-html:"); }
        }

        public string OutputXml
        {
            get { return getValueFromArguments("-xml:"); }
        }

        public string CoverageOutputFile
        {
            get { return getValueFromArguments("-outputfile:"); }
        }

        public string CoverageXslt
        {
            get { return getValueFromArguments("-xslt:"); }
        }

        public bool ProvideHelp { get; set; }

        public ArgumentParser(string[] args)
        {
            var keys = args.Select(a => a.Split(':')[0]);
            var validator = new Validator();
            validator.CheckThat(() => args)
                .IsNotNull<ArgumentNullException>(new[] {"Please check the documentation or type -help to learn more."});
            if (keys.Contains("-help"))
            {
                ProvideHelp = true;
                return;
            }
            validator.CheckThat(() => keys).Contains(new[] {"-assemblies", "-runner", "-tests"});
            validator.ThrowFirst();

            _args = args;
        }
    }
}